package com.dlyk.web;

import com.dlyk.model.DicValue;
import com.dlyk.result.R;
import com.dlyk.service.DicValueService;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 字典值
 */
@RestController
public class DicValueController {

    @Resource
    private DicValueService dicValueService;

    /**
     * 获取联系方式字典数据
     */
    @GetMapping("/api/dicValue/loadNoteWay")
    public R loadNoteWay() {

        List<DicValue> dicValueList = dicValueService.getNoteWay();

        return R.OK(dicValueList);
    }

    /**
     * 查询字典数据列表（分页查询）
     */
    @PreAuthorize(value = "hasAuthority('dicValue:list')")
    @GetMapping("/api/dicValue/list")
    public R dicValueList(@RequestParam(required = false) Integer current,
                          String typeCode) {

        // required = false  表示非必须，这个参数也可以不传，不传参数，默认查询第一页
        if (current == null) {
            current = 1;
        }

        PageInfo<DicValue> dicValuePageInfo = dicValueService.getDicValueListByPage(current, typeCode);

        return R.OK(dicValuePageInfo);
    }

    /**
     * 录入字典数据
     */
    @PreAuthorize(value = "hasAuthority('dicValue:add')")
    @PostMapping("/api/dicValue/add")
    public R addDicValue(DicValue dicValue) {

        // 这里就不使用Query对象接收了，直接使用DicValue对象接收

        int add = dicValueService.addDicValue(dicValue);

        return add >= 1 ? R.OK() : R.FAIL();
    }

    /**
     * 编辑字典数据
     */
    @PreAuthorize(value = "hasAuthority('dicValue:edit')")
    @PutMapping("/api/dicValue/edit")
    public R editDicValue(DicValue dicValue) {

        // 这里就不使用Query对象接收了，直接使用DicValue对象接收

        int edit = dicValueService.editDicValue(dicValue);

        return edit >= 1 ? R.OK() : R.FAIL();
    }

    /**
     * 删除字典数据
     */
    @PreAuthorize(value = "hasAuthority('dicValue:remove')")
    @DeleteMapping("/api/dicValue/remove/{id}")
    public R removeDicValue(@PathVariable Integer id) {

        // 这里就不使用Query对象接收了，直接使用DicValue对象接收

        int remove = dicValueService.removeDicValueById(id);

        return remove >= 1 ? R.OK() : R.FAIL();
    }

    /**
     * 获取是否贷款字典数据
     */
    @GetMapping("/api/dicValue/needLoan")
    public R loadNeedLoan() {

        List<DicValue> dicValueList = dicValueService.getNeedLoan();

        return R.OK(dicValueList);
    }

    /**
     * 获取交易阶段下拉选项
     */
    @GetMapping("/api/dicValue/stage")
    public R loadStage() {

        List<DicValue> dicValueList = dicValueService.getStage();

        return R.OK(dicValueList);
    }

}

